<?
	   layout("/inc/win_layout.html",{title:sysTitle}){
?>
<div class="panel" style="height: 580px; overflow: auto;">
	<div class="panel-body pan">
		<div class="row" style="margin-bottom: 10px;">
			<div class="col-lg-4"
				style="width: 400px; float:left; margin-right:-30px; display:inline;">
				<label for="inputUsername" class="col-md-3 control-label">子系统 <span class='require'>*</span>
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<select id="systemSpan" class="form-control" onchange="getModuleList(this.value);">
<!-- 							<option value="">--请选择--</option> -->
						</select>
					</div>
				</div>
			</div>
			<div class="col-lg-4"
				style="width: 400px; float:left; margin-right:-30px; display:inline;">
				<label for="inputUsername" class="col-md-3 control-label">功能模块 <span class='require'>*</span>
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<select id="moduleSpan" class="form-control" onchange="getTaskList(this.value);">
							<option value="">--请选择--</option>
						</select>
					</div>
				</div>
			</div>
			
		</div>
		<div class="row" style="margin-bottom: 10px;">
		<div class="col-lg-6"
				style="width: 400px; float:left; margin-right:-30px; display:inline;">
				<label for="inputUsername" class="col-md-3 control-label">执行任务 <span class='require'>*</span>
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<select id="taskSpan" class="form-control" onchange="settingExecutorParam(this.value)">
							<option value="">--请选择--</option>
						</select>
					</div>
				</div>
			</div>
			<div class="col-lg-6"
				style="width: 400px; float:left; margin-right:-20px; display:inline;">
				<label for="inputUsername" class="col-md-3 control-label">执行器 <span class='require'>*</span>
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<select id="jobGroup" class="form-control">
						</select>
					</div>
				</div>
			</div>
			
		</div>
		<div class="row" style="margin-bottom: 10px;">
			
			
			<div class="col-lg-4"
				style="width: 200px; float:left; margin-right:-40px; display:none;">
				<label for="inputUsername" class="col-md-3 control-label">批量单次执行数量 <span class='require'>*</span>
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<input id="number" type="text" class="form-control" value="10000" placeholder='请输入正数'>
					</div>
				</div>
			</div>
			<div class="col-lg-4"
				style="width: 200px; float:left; margin-right:-40px; display:none;">
				<label for="inputUsername" class="col-md-3 control-label">线程数量 <span class='require'>*</span>
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<input id="threadsNum" type="text" class="form-control" value="1" placeholder='请输入正数'>
					</div>
				</div>
			</div>
			<div class="col-lg-4"
				style="width: 200px; float:left; margin-right:-40px; display:none;">
				<label for="inputUsername" class="col-md-3 control-label">大数据方式发布 <span class='require'>*</span>
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<select id="releaseStyle" class="form-control">
							<option value="false" selected="selected">否</option>
							<option value="true">是</option>
						</select>
					</div>
				</div>
			</div>
			
			<div class="col-lg-4"
				style="width: 250px; float:left; margin-right:-40px; display:inline;">
				<label for="inputUsername" class="col-md-3 control-label">路由策略 <span class='require'>*</span>
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<select id="executorRouteStrategy" class="form-control">
						</select>
					</div>
				</div>
			</div>
		</div>
		<div class="row" style="margin-bottom: 10px;">
			<div class="col-lg-6" style="width: 400px; float:left; margin-right:-20px; display:inline;">
				<label for="inputUsername" class="col-md-3 control-label">JobHandler <span class='require'>*</span>
				</label>
				<div class="col-md-9 mbm">
					<div class="input-group">
						<input id="executorHandler" type="text" class="form-control" value="dalJobHandler" placeholder='请输入"JobHandler"'>
						<span class="input-group-btn"><label class="checkbox-inline"><button type="button" class="btn btn-default btn-outlined" id="glue"> GLUE&nbsp; <input id="glueSwitch" type="checkbox" value="0"/></button></label></span>
					</div>
				</div>
			</div>
			<div class="col-lg-6"
				style="width: 400px; float:left; margin-right:-20px; display:inline;">
				<label for="inputUsername" class="col-md-3 control-label">Cron <span class='require'>*</span>
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<input id="jobCron" type="text" class="form-control" value="0 0 1 * * ?" placeholder='请输入"Cron"'/>
					</div>
				</div>
			</div>
		</div>
		<div class="row" style="margin-bottom: 10px; display: none;">
			<div class="col-lg-6"
				style="width: 400px; float:left; margin-right:-20px; display:inline;">
				<label for="inputUsername" class="col-md-3 control-label">执行参数
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<input id="executorParam" type="text" class="form-control" value="" placeholder='请输入执行参数' />
					</div>
				</div>
			</div>
			<div class="col-lg-6"
				style="width: 400px; float:left; margin-right:-20px; display:inline;">
				<label for="inputUsername" class="col-md-3 control-label">子任务key
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<input id="childJobKey" type="text" class="form-control" value=""  placeholder='请输入子任务的任务可以，存在多个逗号隔开'/>
					</div>
				</div>
			</div>
		</div>
		<div id="showSeparator" style="margin-bottom: 30px;width: 750px;">
			<label for="inputUsername" class="col-md-3 control-label">任务描述 <span class='require'>*</span>
			</label>
			<div class="col-md-12">
				<div class="input-icon">
					<textarea id="jobDesc" rows="3" cols="40" class="form-control"  placeholder='请输入任务描述'></textarea>
				</div>
			</div>
		</div>
	</div>
<input type="hidden" name="temp" id="temp"/>
<input type="hidden" name="id" id="id"/>
<input id="glueRemark" type="hidden" name="glueRemark" value="GLUE代码初始化" >
<textarea id="glueSource" name="glueSource" style="display:none;" >
package com.xxl.job.service.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xxl.job.core.handler.IJobHandler;

public class DemoGlueJobHandler extends IJobHandler {
	private static transient Logger logger = LoggerFactory.getLogger(DemoGlueJobHandler.class);

	@Override
	public void execute(String... params) throws Exception {
		logger.info("XXL-JOB, Hello World.");
	}

}
</textarea>
	<br> <br> <br>
</div>
<?} ?>
<script>
	var index1 = window.parent.getlay();
	$("#id").val(index1);
	$(function() {
 		if (index1 != 0) {
 			demo1();
 		}
 		getSystemList();
		jobGroup();
		routeStrategy();
	});
	$("#glueSwitch").on('ifChecked', function(event){ //原先为选中状态，点击执行后为未选中
		 $("#executorHandler").val("");
	      $("#executorHandler").attr("disabled","true");
	});
	 $("#glueSwitch").on('ifUnchecked', function (event) {//原先为未选中状态，点击执行后为选中
	     $("#executorHandler").val($("#temp").val());
		 $("#executorHandler").removeAttr("disabled");
     });
	function Save(){
		var url="./xxljob_add.do";
		if($.trim($("#executorHandler").val()).length<1) {
			if(!$("#glueSwitch").is(":checked")){
	    	   	layer.msg("JobHandler不能为空！", {icon: 7});
	       		return null;
			}
	    }
		if($.trim($("#jobCron").val()).length<1) {
    		layer.msg("Cron不能为空！", {icon: 7});
       		return null;
	    }
		if($.trim($("#jobDesc").val()).length<1) {
    	 	layer.msg("任务描述不能为空！", {icon: 7});
       		return null;
	    }
		if (index1 == 0){//新增
			url="./xxljob_add.do";
		}else{//编辑
			url="./xxljob_reschedule.do";
		}
		$.ajax({
			type : "post",
			url : url,
			data:{
				"id":$("#id").val(),
				"executorHandler":$("#executorHandler").val(),
				"jobCron":$("#jobCron").val(),
				"jobDesc":$("#jobDesc").val(),
				"jobGroup":$("#jobGroup option:selected").val(),
				"glueRemark":$("#glueRemark").val(),
				"glueSource":$("#glueSource").text(),
				"executorParam":$("#executorParam").val()+"#"+$("#number").val()+"#"+$("#threadsNum").val()+"#"+$("#releaseStyle option:selected").val(),
				"childJobKey":$("#childJobKey").val(),
				"executorRouteStrategy":$("#executorRouteStrategy option:selected").val(),
				"glueSwitch":$("#glueSwitch").is(":checked")?1:0,
			},
			dataType : "json",
			async : false,
			success : function(data) {
				if(data.result =="ok"){
					 var index = parent.layer.getFrameIndex(window.name); 
			         window.parent.get();
			         parent.layer.msg("操作成功!", {icon: 6});
			         parent.layer.close(index); 
				}else{
					layer.msg(data.result, {icon: 5});
				}
			},
			error : function(data) {
				layer.msg("请求失败！", {icon: 2});
			}
		});
	}
	//获取路由策略
	function routeStrategy(){
		$.ajax({
			type : "post",
			url : "./xxljob_routeStrategy.do",
			data : {
			},
			dataType : "json",
			async : false,
			success : function(data) {
				var html="";
				var dataRole = eval(data);
				for(var i= 0; i< dataRole.length;i++){
					html+='<option value="'+dataRole[i].value+'">'+dataRole[i].title+'</option>';
				}
				$("#executorRouteStrategy").append(html); 
			},
			error : function(data) {
				layer.msg("请求失败！", {icon: 2});
			}
		});
	}
	//获取执行器
	function jobGroup(){
		$.ajax({
			type : "post",
			url : "./xxljob_jobGroup.do",
			data : {
			},
			dataType : "json",
			async : false,
			success : function(data) {
				var html="";
				var dataRole = eval(data);
				for(var i= 0; i< dataRole.length;i++){
					html+='<option value="'+dataRole[i].id+'">'+dataRole[i].title+'</option>';
				}
				$("#jobGroup").append(html); 
			},
			error : function(data) {
				layer.msg("请求失败！", {icon: 2});
			}
		});
	}

function demo1() {
	$.getJSON('./xxljob_editInit.do', {
		id : index1
	//向服务端传的参数，此处只是演示
	}, function(res) {
		//此处仅仅是为了演示变化的内容
		$("#id").val(res[0].id);
		//$("#jobGroup").val(res[0].jobGroup);
		$("#jobGroup").find("option[value='"+res[0].jobGroup+"']").attr("selected",true);
		//$("#executorRouteStrategy").val(res[0].executorRouteStrategy);
		$("#executorRouteStrategy").find("option[value='"+res[0].executorRouteStrategy+"']").attr("selected",true);
		
		$("#executorHandler").val(res[0].executorHandler);
		$("#temp").val(res[0].executorHandler);
		if(res[0].glueSwitch == "1"){
			$("#glueSwitch").attr("checked","checked");
			$("#glueSwitch").prop("checked",true).parent("div").addClass("checked");
			//$(".icheckbox_minimal-grey").attr("class","icheckbox_minimal-grey checked");
			$("#executorHandler").attr("disabled","true");
		}
		var params=res[0].executorParam.split('#');
		
		$("#glueSwitch").val(res[0].glueSwitch);
		$("#jobCron").val(res[0].jobCron);
		$("#executorParam").val(params[0]+"#"+params[1]+"#"+params[2]);
		$("#childJobKey").val(res[0].childJobKey);
		$("#jobDesc").val(res[0].jobDesc);
		$("#glueRemark").val(res[0].glueRemark);
		$("#glueSource").text(res[0].glueSource);
		
		
		
		if(params[0]!=undefined){
			$("#systemSpan").val(params[0]);
			getModuleList(params[0]);
		}
		if(params[1]!=undefined){
			$("#moduleSpan").val(params[1]);
			getTaskList(params[1]);
		}
		if(params[2]!=undefined){
			$("#taskSpan").val(params[0]+"#"+params[1]+"#"+params[2]);
		}
		if(params[3]!=undefined){
			$("#number").val(params[3]);
		}
		if(params[4]!=undefined){
			$("#threadsNum").val(params[4]);
		}
		if(params[5]!=undefined){
			$("#releaseStyle").val(params[5]);
		}
	});
};
var systemEn='';
var moduleEn='';
//获得子系统
function getSystemList() {
	$.ajax({
		type : "post",
		url : "system-list.html",
		dataType : "json",
		async : false,
		success : function(data) {
			var html="<option value=''>--请选择--</option>";
			var dataEntity = eval(data);
			for(var i= 0; i< dataEntity.length;i++){
				html+='<option value="'+dataEntity[i].systemEn+'">'+dataEntity[i].systemZh+'</option>';
			}
			$("#systemSpan").append(html); 
		},
		error : function(data) {
			layer.msg("请求失败！", {icon: 2});
		}
	});
}
//获得子系统-功能模块
function getModuleList(value) {
	$("#jobDesc").text($("#systemSpan option:selected").text());
	$("#moduleSpan option").remove();
	systemEn = value;
	$.ajax({
		type : "post",
		url : "module-list.html",
		data : {
			"systemEn":systemEn
		},
		dataType : "json",
		async : false,
		success : function(data) {
			var html="<option value=''>--请选择--</option>";
			var dataEntity = eval(data);
			for(var i= 0; i< dataEntity.length;i++){
				html+='<option value="'+dataEntity[i].moduleEn+'">'+dataEntity[i].moduleZh+'</option>';
			}
			$("#moduleSpan").append(html); 
		},
		error : function(data) {
			layer.msg("请求失败！", {icon: 2});
		}
	});
}
//获任务
function getTaskList(value) {
	$("#jobDesc").text($("#systemSpan option:selected").text()+">>"+$("#moduleSpan option:selected").text());
	$("#taskSpan option").remove();
	moduleEn = value;
	$.ajax({
		type : "post",
		url : "task-list.html",
		data : {
			"systemEn":systemEn,
			"moduleEn":moduleEn
		},
		dataType : "json",
		async : false,
		success : function(data) {
			var html="<option value=''>--请选择--</option>";
			var dataEntity = eval(data);
			for(var i= 0; i< dataEntity.length;i++){
				html+='<option value="'+dataEntity[i].taskValue+'">'+dataEntity[i].taskName+'</option>';
			}
			$("#taskSpan").append(html); 
		},
		error : function(data) {
			layer.msg("请求失败！", {icon: 2});
		}
	});
}

function settingExecutorParam(value) {
	$("#executorParam").val(value);
	$("#jobDesc").text($("#systemSpan option:selected").text()+">>"+$("#moduleSpan option:selected").text()+">>"+$("#taskSpan option:selected").text());
}
</script>


